package com.starinside.wp.service.impl;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.starinside.wp.foundation.model.StatusResponse;
import com.starinside.wp.foundation.vo.ChildrenVo;
import com.starinside.wp.foundation.vo.TeachingMaterialVo;
import com.starinside.wp.foundation.vo.TeachingPlanVo;
import com.starinside.wp.service.ChildrenCourseService;
import com.starinside.wp.utils.DateUtil;
import com.starinside.wp.utils.MapConvertUtil;
import com.starinside.wp.utils.RemoteCallUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Describle This Class Is
 * @Author ZengMin
 * @Date 2018/11/16 16:09
 * @Company Matt
 */
@Service
public class ChildrenCourseServiceImpl implements ChildrenCourseService {

    @Value("${teach.server.url}")
    String teachUrl;

    private static final String GEI_CHILDREN_URI = "/api/phone/user/children/getChildrenInfo";
    private static final String GEI_TEACHING_PLAN_URI = "/api/phone/user/children/getTeachingPlan";
    private static final String GEI_TEACHING_PLAN_INFO_URI = "/api/phone/user/children/getTeachingPlanInfo";
    private static final String EVALUATING_LIST_URI = "/api/phone/user/children/getChildEvaluatingList";
    private static final String SATCOURSE_LIST_URI = "/api/phone/user/children/getSatCourse";

//    @Value("${mp.server.url}")
//    String mpUrl;

    @Override
    public ChildrenVo getChildrenInfo(String cid) {
        ChildrenVo childrenVo = RemoteCallUtil.call(teachUrl + GEI_CHILDREN_URI, ImmutableMap.of("cid", cid), null, ChildrenVo.class, "宝宝不存在！");
        return childrenVo;
    }

    @Override
    public List<TeachingPlanVo> getGrowPlan(String cid, int addDay) {
        List<HashMap> teachingPlanVos = RemoteCallUtil.callOfList(teachUrl + GEI_TEACHING_PLAN_URI, ImmutableMap.of("cid", cid, "addDay", addDay), null, HashMap.class, "教务系统 通讯异常");
            if(teachingPlanVos.size() > 0){
                List<TeachingPlanVo> resultList = Lists.newArrayList();
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
                teachingPlanVos.stream().forEach( m ->{
                    TeachingPlanVo o = MapConvertUtil.mapToObject(m, TeachingPlanVo.class);
                    o.setEndTime(simpleDateFormat.format(o.getToTime()));
                    o.setBeginTime(simpleDateFormat.format(o.getFromTime()));
//                    o.setUrl(teachUrl + "/schemeDetail/" + ticket + "/" + o.getMaterialId());
                    resultList.add(o);
                });
                return resultList;
            }
        return null;
    }

    @Override
    public TeachingMaterialVo getGrowPlanInfo(long id) {
        TeachingMaterialVo teachingMaterialVo = RemoteCallUtil.call(teachUrl + GEI_TEACHING_PLAN_INFO_URI, ImmutableMap.of("id", id),null,TeachingMaterialVo.class,"教务系统 通讯异常");
        return teachingMaterialVo;
    }

    @Override
    public StatusResponse getChildEvaluatingList(String childrenId) {
        List<HashMap> list = RemoteCallUtil.callOfList(teachUrl + EVALUATING_LIST_URI, ImmutableMap.of("cid", childrenId), null, HashMap.class, "教务系统 通讯异常");
        if (null != list && list.size() > 0) {
            List<Map<String, Object>> result = Lists.newArrayList();
            list.stream().forEach(map -> {
                String created = map.get("created").toString();
                String id = map.get("id").toString();
                map.clear();
                String dateTime = DateUtil.millisToDateTime(Long.valueOf(created), "yyyy-MM-dd HH:mm:ss");
                map.put("created", dateTime);
                map.put("title", "能力发展评测");
                map.put("linkId", id);
//                map.put("url", mpUrl + "/feed/detail_evaluate?linkId=" + id);
                result.add(map);
            });
            return StatusResponse.success(result);
        }
        return null;
    }

    @Override
    public StatusResponse getSatCourse(String childrenId) {
        List<HashMap> hashMaps = RemoteCallUtil.callOfList(teachUrl + SATCOURSE_LIST_URI, ImmutableMap.of("cid", childrenId), null, HashMap.class, "教务系统 通讯异常");
        List<Object> result = Lists.newArrayList();
        if (null != hashMaps && hashMaps.size() > 0) {
            hashMaps.stream().forEach(o -> {
                Map<String, Object> map = new HashMap<>();
                String id = o.get("id").toString();
                String courseDate = o.get("courseDate").toString();
                courseDate = DateUtil.millisToDateTime(Long.valueOf(courseDate), "yyyy-MM-dd");
                String courseName = o.get("courseName").toString();
                String beginTime = o.get("beginTime").toString();
                beginTime = DateUtil.millisToDateTime(Long.valueOf(beginTime), "yyyy-MM-dd HH:mm:ss");
                String endTime = o.get("endTime").toString();
                endTime = DateUtil.millisToDateTime(Long.valueOf(endTime), "yyyy-MM-dd HH:mm:ss");
                String teacherName = o.get("teacherName").toString();
                map.put("id", id);
                map.put("courseDate", courseDate);
                map.put("courseName", courseName);
                map.put("beginTime", beginTime);
                map.put("endTime", endTime);
                map.put("teacherName", teacherName);
                result.add(map);
            });
        }
        return StatusResponse.success(result);
    }
}
